/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.event;


import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import com.intelligent.ispc.client.MainFrame;
import com.intelligent.ispc.client.core.view.LoginPanel;
import com.intelligent.ispc.client.core.view.analysis.grr.GrrMainPanel;
import com.intelligent.ispc.client.core.view.analysis.report.YieldPanel;
import com.intelligent.ispc.client.core.view.analysis.spc.SpcMainPanel;
import com.intelligent.ispc.client.core.view.configuration.AnalysisConfigPanel;
import com.intelligent.ispc.client.core.view.global.MainContentPanel;
import com.intelligent.ispc.client.core.view.system.ChangePwdPanel;
import com.intelligent.ispc.client.core.view.system.ScriptTestPanel;
import com.intelligent.ispc.client.core.view.system.SystemConfigPanel;
import com.intelligent.ispc.client.utils.ApplicationBeanUtils;
import com.intelligent.ispc.client.utils.EventBusUtils;
import com.intelligent.ispc.client.utils.ResourceMessages;

import javax.swing.*;

/**
 * Created by Peter on 2016/7/26.
 */
public class PanelChangeListener {

    /**
     * Subscribe the event for changing panel.
     *
     * @param event PanelChangeEvent
     */
    @Subscribe
    @AllowConcurrentEvents
    public void changePanel(PanelChangeEvent event) {
        try {
            change(event);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void change(PanelChangeEvent event) {
        Class panel = event.getPanel();
        boolean inContent = event.getInContentPanel();
        String panelName = event.getPanelName();

        if (panel == null) {
            return;
        }

        JPanel currentPanel = (JPanel) ApplicationBeanUtils.getBean(MainContentPanel.class).getComponent(0);
        JPanel systemPanel = (JPanel) ApplicationBeanUtils.getBean(AnalysisConfigPanel.class);
        if (currentPanel == systemPanel) {
            if (!((AnalysisConfigPanel) systemPanel).changePanelMessage()) {
                return;
            }
        }

        EventBusUtils.post(new ExportBtnEnabledEvent(false));

        if (inContent) {
            if (panel == LoginPanel.class) {
                ApplicationBeanUtils.getBean(MainFrame.class).changePanel(panel);
            } else {
                menuChangeData(panelName);
                ApplicationBeanUtils.getBean(MainContentPanel.class).changePanel(panel);
            }
        } else {
            ApplicationBeanUtils.getBean(MainFrame.class).changePanel(panel);
        }
    }

    private void menuChangeData(String panelName) {
        if (ResourceMessages.MENU_ANALYSIS_SPC.equals(panelName)) {
            ApplicationBeanUtils.getBean(SpcMainPanel.class).initData();
        } else if (ResourceMessages.MENU_ANALYSIS_REPORT.equals(panelName)) {
            ApplicationBeanUtils.getBean(YieldPanel.class).initData();
        } else if (ResourceMessages.MENU_ANALYSIS_GRR.equals(panelName)) {
            ApplicationBeanUtils.getBean(GrrMainPanel.class).initData();
        } else if (ResourceMessages.MENU_CONFIG_ANALYSIS.equals(panelName)) {
            ApplicationBeanUtils.getBean(AnalysisConfigPanel.class).initData();
        } else if (ResourceMessages.MENU_SYSTEM_SETTINGS.equals(panelName)) {
            ApplicationBeanUtils.getBean(SystemConfigPanel.class).initData();
        } else if (ResourceMessages.MENU_SYSTEM_CHANGE_PASSWORD.equals(panelName)) {
            ApplicationBeanUtils.getBean(ChangePwdPanel.class).initData();
        } else if (ResourceMessages.MENU_SYSTEM_SCRIPT.equals(panelName)) {
            ApplicationBeanUtils.getBean(ScriptTestPanel.class).initData();
        }
    }

}
